package cn.taketoday.live.handler;

import com.alibaba.fastjson.JSONObject;

import cn.taketoday.live.WebSocketMessage;
import cn.taketoday.live.config.GlobalData;
import cn.taketoday.live.data.BlockMessage;
import cn.taketoday.live.data.enums.ShieldMessage;
import cn.taketoday.live.utils.JodaTimeUtils;

/**
 * @author TODAY 2021/5/17 23:23
 */
public class RoomBlockMessageCommandHandler implements CommandHandler {

  @Override
  public void handle(CommandContext context) throws Exception {
    final JSONObject jsonObject = context.getOriginal();
    final StringBuilder stringBuilder = new StringBuilder(32);
    if (context.contains(ShieldMessage.is_block)) {
      BlockMessage blockMessage = JSONObject.parseObject(jsonObject.getString("data"), BlockMessage.class);
      stringBuilder.append(JodaTimeUtils.getCurrentDateTimeString());
      stringBuilder.append(":禁言消息:");
      stringBuilder.append(blockMessage.getUname());
      if (blockMessage.getOperator() == 2) {
        stringBuilder.append("已被主播禁言");
      }
      else if (blockMessage.getOperator() == 1) {
        stringBuilder.append("已被房管禁言");
      }
      else {
        stringBuilder.append("已被管理员禁言");
      }
      //控制台打印\
      if (context.contains(ShieldMessage.is_cmd)) {
        System.out.println(stringBuilder.toString());
      }
      context.sendMessage(WebSocketMessage.of("block", blockMessage));
      if (GlobalData.logThread != null && !GlobalData.logThread.FLAG) {
        GlobalData.logString.add(stringBuilder.toString());
        synchronized(GlobalData.logThread) {
          GlobalData.logThread.notify();
        }
      }
    }
//	 LOGGER.debug("谁这么惨被禁言了:::" + message);
  }

}
